package com.xbongbong.saas.help;

import com.alibaba.fastjson.JSON;
import com.kuaidi100.sdk.api.AutoNum;
import com.kuaidi100.sdk.api.CloudPrintOld;
import com.kuaidi100.sdk.api.PrintCloud;
import com.kuaidi100.sdk.api.PrintHtml;
import com.kuaidi100.sdk.api.PrintImg;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.api.QueryTrackMap;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.AutoNumReq;
import com.kuaidi100.sdk.request.CloudPrintOldParam;
import com.kuaidi100.sdk.request.PrintCloudParam;
import com.kuaidi100.sdk.request.PrintHtmlParam;
import com.kuaidi100.sdk.request.PrintImgParam;
import com.kuaidi100.sdk.request.PrintReq;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import org.springframework.stereotype.Component;

/**
 * 快递100 helper
 * lee
 */
@Component
public class ExpressHelper {

    /**
     * 查询物流轨迹
     * @param accountKey
     * @param customer
     * @param queryTrackParam  com,num,phone
     * @return
     * @throws Exception
     */
    public static HttpResult queryTrack(String accountKey, String customer, QueryTrackParam queryTrackParam) throws Exception {
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        String param = JSON.toJSONString(queryTrackParam);
        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param ,accountKey,customer));
        IBaseClient baseClient = new QueryTrack();
        return baseClient.execute(queryTrackReq);
    }

    /**
     * 智能识别接口
     * @param accountKey
     * @param expressCode 快递单号
     * @return
     * @throws Exception
     */
    public static HttpResult autoQueryTrack(String accountKey, String expressCode) throws Exception {
        AutoNumReq autoNumReq = new AutoNumReq();
        autoNumReq.setKey(accountKey);
        autoNumReq.setNum(expressCode);
        IBaseClient baseClient = new AutoNum();
        return baseClient.execute(autoNumReq);
    }

    /**
     * 电子面单云打印
     * @param accountKey
     * @param secret
     * @param printCloudParam recMan, sendMan, kuaidicom, count, tempid, siid
     * @return
     * @throws Exception
     */
    public static HttpResult cloudPrint(String accountKey, String secret, PrintCloudParam printCloudParam) throws Exception {
        String param = JSON.toJSONString(printCloudParam);
        String t = System.currentTimeMillis() + "";
        PrintReq printReq = new PrintReq();
        printReq.setT(t);
        printReq.setKey(accountKey);
        printReq.setMethod(ApiInfoConstant.ELECTRONIC_ORDER_PRINT_METHOD);
        printReq.setSign(SignUtils.printSign(param,t,accountKey,secret));
        printReq.setParam(param);
        IBaseClient baseClient = new PrintCloud();
        return baseClient.execute(printReq);
    }

    /**
     * 电子面单复打
     * @param accountKey
     * @param secret
     * @param cloudPrintOldParam taskId
     * @return
     * @throws Exception
     */
    public static HttpResult cloudPrintOld(String accountKey, String secret, CloudPrintOldParam cloudPrintOldParam) throws Exception {
        String t = System.currentTimeMillis() + "";
        String param = JSON.toJSONString(cloudPrintOldParam);
        PrintReq printReq = new PrintReq();
        printReq.setKey(accountKey);
        printReq.setMethod(ApiInfoConstant.CLOUD_PRINT_OLD_METHOD);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param,t,accountKey,secret));
        printReq.setParam(param);
        IBaseClient cloudPrintOld = new CloudPrintOld();
        return cloudPrintOld.execute(printReq);
    }

    /**
     * 快递信息地图轨迹
     * @param accountKey
     * @param customer
     * @param queryTrackParam num, phone, form, to, resultv2, com
     * @return
     * @throws Exception
     */
    public static HttpResult queryMapView(String accountKey, String customer, QueryTrackParam queryTrackParam) throws Exception {
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        String t = System.currentTimeMillis() + "";
        String param = JSON.toJSONString(queryTrackParam);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param,accountKey,customer));
        queryTrackReq.setParam(param);
        IBaseClient baseClient = new QueryTrackMap();
        return baseClient.execute(queryTrackReq);
    }

    /**
     * 电子面单html接口
     * @param accountKey
     * @param secret
     * @param printHtmlParam recMan, sendMan, kuaidicom, count, tempid, needTemplate, needChild
     * @return
     * @throws Exception
     */
    public static HttpResult printHtml(String accountKey, String secret, PrintHtmlParam printHtmlParam) throws Exception {
        String t = System.currentTimeMillis() + "";
        String param = JSON.toJSONString(printHtmlParam);
        PrintReq printReq = new PrintReq();
        printReq.setKey(accountKey);
        printReq.setMethod(ApiInfoConstant.ELECTRONIC_ORDER_HTML_METHOD);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param,t,accountKey,secret));
        printReq.setParam(param);
        IBaseClient printHtml = new PrintHtml();
        return printHtml.execute(printReq);
    }

    /**
     * 电子面单图片接口
     * @param accountKey
     * @param secret
     * @param printImgParam
     * @return
     * @throws Exception
     */
    public static HttpResult printImg(String accountKey, String secret, PrintImgParam printImgParam) throws Exception {
        String t = System.currentTimeMillis() + "";
        String param = JSON.toJSONString(printImgParam);
        PrintReq printReq = new PrintReq();
        printReq.setKey(accountKey);
        printReq.setMethod(ApiInfoConstant.ELECTRONIC_ORDER_PIC_METHOD);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param,t,accountKey,secret));
        printReq.setParam(param);
        IBaseClient printImg = new PrintImg();
        return printImg.execute(printReq);
    }

}
